window._MAPSELECT = {

    _MAINLABEL:null,

    _CURRENTAREALABEL:null,

    _MAPINDEX:null,


    getMapIndex:function(){
        var maptree = _MAPHELPER.getMapTree();
        var currentMapName = gameData.defaultMap;
        if(maptree[currentMapName]){
            _MAPSELECT._MAPINDEX = currentMapName;
        }else if(
            _MAPCONFIG[currentMapName]
            &&_MAPCONFIG[currentMapName].parent
            &&maptree[_MAPCONFIG[currentMapName].parent]
        ){
            _MAPSELECT._MAPINDEX=_MAPCONFIG[currentMapName].parent;
        }else if(
            window.gameData.lastmap
            &&_MAPCONFIG[window.gameData.lastmap]
            &&_MAPCONFIG[window.gameData.lastmap].parent
            &&maptree[_MAPCONFIG[window.gameData.lastmap].parent]
        ){
            _MAPSELECT._MAPINDEX = _MAPCONFIG[window.gameData.lastmap].parent;
        }else{
            _MAPSELECT._MAPINDEX='中部';
        }

        //console.log('当前地图:',currentMapName,_MAPSELECT._MAPINDEX);
    },


    closeUi:function(){
        if(_MAPSELECT._MAINLABEL){
            _GRM.putNode(_MAPSELECT._MAINLABEL);
        }
    },

    showUi:function(parentNode,param){

        var uiObj = _GRM.getNodeObj('mapSelect');
        var mainLabel = uiObj.node;
        _MAPSELECT._MAINLABEL = mainLabel;
        _MAPSELECT._CURRENTAREALABEL = null;
        _MAPSELECT._MAPINDEX =null;

        if(uiObj.isnew){
            _MAPSELECT.initButton(mainLabel);
        }

        parentNode.addChild(mainLabel);

        _MAPSELECT.refresh();

        return mainLabel;

    },

    initButton:function(node){
        node.getChildByName('mask').on(cc.Node.EventType.TOUCH_START, function(){
            _GRM.putNode(this);
        }, node);

        var btns = node.getChildByName('bigMap')
                               .getChildByName('view')
                               .getChildByName('content')
                               .getChildren();
        
        var labels = node.getChildByName('cityMap')
                               .getChildByName('scrollview')
                              .getChildByName('view')
                              .getChildByName('content')
                              .getChildren();
        
        for(var i=0;i<btns.length;i++){
            btns[i].on(cc.Node.EventType.TOUCH_START, function(){
                //console.log(this.mapname);
                _MAPSELECT._MAPINDEX=this.mapname;
                _MAPSELECT.refreshRight();
            }, btns[i]);
        }

        for(var i=0;i<labels.length;i++){
            var labelBtn = labels[i].getChildByName('button');
            labelBtn.on(cc.Node.EventType.TOUCH_START, function(){
                //console.log(this.cityForJump);
                if(_MAP.canJumpToMap(this.cityForJump,'默认')){
                    _MAP.jumpToMap(this.cityForJump,'默认');
                }
                _MAPSELECT.closeUi();
            }, labels[i]);
        }

        var bgMap = node.getChildByName('bgMap');
        var areas = bgMap.getChildByName('map').getChildren();

        for(var i=0;i<areas.length;i++){

            
            var tempAreaBtn = areas[i];
            var tempAreaChapter = tempAreaBtn.getChildByName('label').getComponent(cc.Label).string;
            tempAreaBtn.chapter = tempAreaChapter;
            tempAreaBtn.on(cc.Node.EventType.TOUCH_START, function(){
                console.log(this.chapter);
            }, tempAreaBtn);
        }
    },

    refresh:function(){
        _MAPSELECT.getMapIndex();
        _MAPSELECT.refreshLeft();
        _MAPSELECT.refreshRight();
    },

    //刷新大图列表
    refreshLeft:function(){
        if(_MAPSELECT._MAINLABEL){
            var mainNode = _MAPSELECT._MAINLABEL;
            var btns = mainNode.getChildByName('bigMap')
                               .getChildByName('view')
                               .getChildByName('content')
                               .getChildren();
            var maptree = _MAPHELPER.getMapTree();
            var btnIndex = 0;
            for(var key in maptree){

                /*
                if(btnIndex==0){
                    _MAPSELECT._MAPINDEX=key;
                }

                if(key==gameData.defaultMap){
                    _MAPSELECT._MAPINDEX=key;
                }
                */

                var mapConfig = _MAPCONFIG[key];
                var btn = btns[btnIndex];
                if(btn&&mapConfig){
                    btn.mapname = key;
                    btn.getChildByName('Background').getChildByName('Label')
                    .getComponent(cc.Label).string = mapConfig.name;
                }
                btnIndex++;
            }

        }
    },

    //刷新城池列表
    refreshRight:function(){
        if(_MAPSELECT._MAINLABEL){
            var mainNode = _MAPSELECT._MAINLABEL;
            var maptree = _MAPHELPER.getMapTree();
            var citys = maptree[_MAPSELECT._MAPINDEX];
            var areaLabel = mainNode.getChildByName('bg_xuan').getChildByName('label');
            var labels = mainNode.getChildByName('cityMap')
                                .getChildByName('scrollview')
                               .getChildByName('view')
                               .getChildByName('content')
                               .getChildren();
            
            for(var i=0;i<labels.length;i++){
                if(citys&&citys[i]){
                    labels[i].active = true;
                    _MAPSELECT.refreshCityLabel(labels[i],citys[i]);
                }else{
                    labels[i].active = false;
                }
            }

            this.refreshArea(_MAPSELECT._MAPINDEX);

            this.refreshCurrentPos(_MAPSELECT._MAPINDEX);

            if(_MAPCONFIG[_MAPSELECT._MAPINDEX]){
                areaLabel.getComponent(cc.Label).string = _MAPCONFIG[_MAPSELECT._MAPINDEX].name;
            }else{
                areaLabel.getComponent(cc.Label).string = '???';
            }
        }
    },

    //刷新当前位置
    refreshCurrentPos:function(){

        var mainNode = _MAPSELECT._MAINLABEL;
        var bgMap = mainNode.getChildByName('bgMap');
        var currentPos = bgMap.getChildByName('currentPos');
        if(_MAPSELECT._MAPINDEX==gameData.defaultMap){
            currentPos.active = true;
            var realpos = _MAPSELECT.getCurrentPos();
            if(realpos)
                currentPos.setPosition(realpos);
            else
                currentPos.active = false;
        }else{
            currentPos.active = false;
        }

    },

    //获取当前相对坐标
    getCurrentPos:function(){

        if(_MAPSELECT._CURRENTAREALABEL){

            var mapparam = _MAP.getParamForLittleMap();
            var lmaph = _MAPSELECT._CURRENTAREALABEL.height;
            var lmapw = _MAPSELECT._CURRENTAREALABEL.width;
            var mapcenterx = mapparam.width/2;
            var mapcentery = mapparam.height/2;
            var rx = (mapparam.x-mapcenterx)/mapparam.width*lmapw;
            var ry = -(mapparam.y-mapcentery)/mapparam.height*lmaph;

            return cc.v2(rx,ry);
        }else{
            return null;
        }
    },

    //刷新小地图
    refreshArea:function(bgmapname){

        var mainNode = _MAPSELECT._MAINLABEL;
        var bgMap = mainNode.getChildByName('bgMap');
        var areas = bgMap.getChildByName('areas').getChildren();

        for(var i=0;i<areas.length;i++){

            if(areas[i].getName()!=bgmapname){
                areas[i].active = false;
            }else{
                areas[i].active = true;
                _MAPSELECT._CURRENTAREALABEL = areas[i];
                var citys = areas[i].getChildren();
                for(var j=0;j<citys.length;j++){
                    var cityName = citys[j].getName();
                    if(!_MAPCONFIG[cityName]){
                        citys[j].getChildByName('label').active = true;
                        citys[j].getChildByName('bg_name').active = true;
                        citys[j].getChildByName('mask').active = false;
                    }else{
                        if(_GAMEDATAHELPER.isMapSign(cityName)){
                            citys[j].getChildByName('mask').active = false;
                            citys[j].getChildByName('label').active = true;
                            citys[j].getChildByName('bg_name').active = true;
                            if(_AREAARMY[cityName])
                                citys[j].getChildByName('label').getComponent(cc.Label).string 
                                = cityName+"\nLV."+_AREAARMY[cityName].lv;
                            else
                                citys[j].getChildByName('label').getComponent(cc.Label).string 
                                = cityName+"\nLV.???";
                            
                            if(window.gameData.cityLog[cityName]){
                                citys[j].getChildByName('label').color 
                                = new cc.color(_MASTER[window.gameData.cityLog[cityName]].color);
                            }

                        }else {
                            citys[j].getChildByName('label').active = false;
                            citys[j].getChildByName('bg_name').active = false;
                            citys[j].getChildByName('mask').active = true;
                        }
                    }
                }
            }

        }

    },

    refreshCityLabel:function(node,citykey){

        if(node&&citykey){

            node.cityForJump = citykey;

            var citynameNode = node.getChildByName('cityname');
            var masternameNode = node.getChildByName('mastername');
            var bossnameNode = node.getChildByName('bossname');
            var master = window.gameData.cityLog[citykey];
            var labelBtn = node.getChildByName('button');

            if(_GAMEDATAHELPER.isMapSign(citykey)){
                citynameNode.getComponent(cc.Label).string = citykey;
                if(master&&_MASTER[master]){
                    masternameNode.getComponent(cc.Label).string = _MASTER[master].name;
                    //bossnameNode.getComponent(cc.Label).string = _MASTER[master].boss;
					bossnameNode.getComponent(cc.Label).string = _MASTERHELPER.getRealMasterBoss(master);
                }
                labelBtn.active = true;
            }else{
                citynameNode.getComponent(cc.Label).string = "???";
                masternameNode.getComponent(cc.Label).string = "???";
                bossnameNode.getComponent(cc.Label).string = "???";
                labelBtn.active = false;
            }
            

        }

    }

}